package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.CateGoryService;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author : [wangtao]
 * @className : SetmealServiceImpl
 * @description : []
 * @createTime : [2022/10/9 18:50]
 * @updateTime : [2022/10/9 18:50]
 */
@Service

@Slf4j
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private CateGoryService cateGoryService;

/*
* 新增菜品功能
*
* */
    @Override
    @Transactional
    public R<String> SaveDish(DishDto dish){
    this.save(dish);
        Long id = dish.getId();
        List<DishFlavor> flavors = dish.getFlavors();
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(id);
        }

        dishFlavorService.saveBatch(flavors);
     return   R.success("成功添加菜品");
    }
/*
*菜品分类管理功能
* */
    @Override
    public R<Page<Dish>> pageInfo(int page, int pageSize, String name){
        Page<Dish> pageinf=new Page<>(page,pageSize);
        LambdaQueryWrapper<Dish> lqw =new LambdaQueryWrapper<>();
        lqw.eq(StringUtils.isNotEmpty(name),Dish::getName,name);
//        IPage page221=new Page();
        lqw.orderByDesc(Dish::getSort).orderByDesc(Dish::getCreateTime);
        Page<Dish> pagelist = this.page(pageinf, lqw);
        List<Dish> records = pagelist.getRecords();

        List<Long> collect = records.stream().map(dish -> dish.getCategoryId()).collect(Collectors.toList());

        List<Category> categories = cateGoryService.listByIds(collect);
        Map<Long, String> map = categories.stream().collect(Collectors.toMap(Category::getId, Category::getName));


        for (Dish record : records) {
            Long categoryId = record.getCategoryId();
            String categoryName = map.get(categoryId);
            record.setCategoryName(categoryName);

        }
        return R.success(pagelist);
    }
    @Override
    @Transactional
public void  updatesave(DishDto dish){
        boolean b = this.updateById(dish);
        log.info("B:  {}",b);
        //删除口味表中,菜品原对应的口味
    LambdaQueryWrapper<DishFlavor> lqw  =new LambdaQueryWrapper<>();
    lqw.eq(DishFlavor::getDishId,dish.getId());
    dishFlavorService.remove(lqw);
    //保存更新后菜品对应口味信息
    List<DishFlavor> flavors = dish.getFlavors().stream().map((item) -> {
        item.setDishId(dish.getId());
        return item;
    }).collect(Collectors.toList());
    dishFlavorService.saveBatch(flavors);
}


}
